
package com.wyzz.hr.bo.impl;

import java.util.List;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.IOrgheadsBo;
import com.wyzz.hr.dao.IOrgheadsDao;
import com.wyzz.hr.pojo.Orgheads;


public class OrgheadsBoImpl implements IOrgheadsBo {

	public OrgheadsBoImpl() {
	}

	public boolean insert(Orgheads head) {
		try {
			String hql = (new StringBuilder()).append(
					"delete from Orgheads where orgheadsOrgNo='").append(
					head.getOrgheadsOrgNo()).append("'").toString();
			headsDao.exeHql(hql);
			headsDao.saveObject(head);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean addOrgheads(Orgheads head) {
		String hql2 = (new StringBuilder()).append(
				"select count(*) from Orgheads where orgheadsOrgNo='").append(
				head.getOrgheadsOrgNo()).append("' and orgheadsEmpNo  ='")
				.append(head.getOrgheadsEmpNo()).append("'").toString();
		List check = headsDao.exeHqlList(hql2);
		if (Integer.valueOf(check.get(0).toString()).intValue() > 0)
			return false;
		try {
			check = headsDao.exeHqlList((new StringBuilder()).append(
					"select count(*) from Orgheads where orgheadsOrgNo='")
					.append(head.getOrgheadsOrgNo()).append(
							"' and orgheadsResponsibility like '%header%'")
					.toString());
			if (Integer.valueOf(check.get(0).toString()).intValue() == 0)
				if (head.getOrgheadsResponsibility().equalsIgnoreCase(
						"bradeputy"))
					head.setOrgheadsResponsibility("braheader");
				else if (head.getOrgheadsResponsibility().equalsIgnoreCase(
						"deptdeputy"))
					head.setOrgheadsResponsibility("deptheader");
				else if (head.getOrgheadsResponsibility().equalsIgnoreCase(
						"budeputy"))
					head.setOrgheadsResponsibility("buheader");
				else if (head.getOrgheadsResponsibility().equalsIgnoreCase(
						"grpdeputy"))
					head.setOrgheadsResponsibility("grpheader");
				else if (head.getOrgheadsResponsibility().equalsIgnoreCase(
						"locationdeputy"))
					head.setOrgheadsResponsibility("locationheader");
			headsDao.saveObject(head);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean updateOrgheads(Orgheads head) {
		String hql = (new StringBuilder()).append(
				"select count(*) from Orgheads where orgheadsId != '").append(
				head.getOrgheadsId()).append("' and orgheadsOrgNo='").append(
				head.getOrgheadsOrgNo()).append("' and orgheadsEmpNo='")
				.append(head.getOrgheadsEmpNo()).append("'").toString();
		List check = headsDao.exeHqlList(hql);
		if (Integer.valueOf(check.get(0).toString()).intValue() > 0)
			return false;
		try {
			Orgheads orgheads = (Orgheads) headsDao.loadObject(
					com.wyzz.hr.pojo.Orgheads.class, head.getOrgheadsId(),
					null, new boolean[0]);
			orgheads
					.setOrgheadsResponsibility(head.getOrgheadsResponsibility());
			orgheads.setOrgheadsEmpNo(head.getOrgheadsEmpNo());
			orgheads.setOrgheadsOrgNo(head.getOrgheadsOrgNo());
			headsDao.updateObject(orgheads);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean delOrgheads(Class head, String id) {
		Orgheads orgheads = (Orgheads) headsDao.loadObject(head, id, null,
				new boolean[0]);
		if (orgheads == null)
			return false;
		String deputy = orgheads.getOrgheadsResponsibility();
		if (deputy.endsWith("header")) {
			String hql = "select count(*) from Orgheads";
			hql = (new StringBuilder()).append(hql).append(
					" where orgheadsOrgNo='").append(
					orgheads.getOrgheadsOrgNo()).append("'").toString();
			hql = (new StringBuilder()).append(hql).append(
					" and orgheadsResponsibility like '%deputy%'").toString();
			List check = headsDao.exeHqlList(hql);
			if (Integer.valueOf(check.get(0).toString()).intValue() > 0)
				return false;
		}
		headsDao.deleteObject(orgheads);
		return true;
	}

	public void saveOrgHeadsSortIdByBatch(String ids[]) {
		int length = ids.length;
		int sortId = 1;
		for (int i = 0; i < length; i++) {
			headsDao.exeHql((new StringBuilder()).append(
					"update Orgheads set orgHeadsSortId=").append(sortId)
					.append(" where id ='").append(ids[i]).append("'")
					.toString());
			sortId++;
		}

	}

	public List listOrgheadNo(String orgType) {
		if (orgType == null) {
			return null;
		} else {
			String hql = (new StringBuilder()).append(
					"from Orgheads where orgheadsResponsibility like '")
					.append(orgType).append("%'").toString();
			return headsDao.exeHqlList(hql);
		}
	}

	public List FindAllOrgheads() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Orgheads.class);
		dc.addOrder(Order.asc("orgHeadsSortId"));
		return headsDao.findByCriteria(dc);
	}

	public List FindOrgheadsByEmpId(String empNo) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Orgheads.class);
		dc.add(Restrictions.eq("orgheadsEmpNo", empNo));
		return headsDao.findByCriteria(dc);
	}

	public List exeHqlList(String hql) {
		return headsDao.exeHqlList(hql);
	}

	public List getOrgheadsByOrgId(String orgId) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Orgheads.class);
		dc.add(Restrictions.eq("orgheadsOrgNo", orgId.trim()));
		return headsDao.findByCriteria(dc);
	}

	public IOrgheadsDao getHeadsDao() {
		return headsDao;
	}

	public void setHeadsDao(IOrgheadsDao headsDao) {
		this.headsDao = headsDao;
	}

	IOrgheadsDao headsDao;
}
